package company.tree;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * N 叉树
 */
public class Ntree {
    NTreeNode root;

    public static void main(String[] args) {
        Ntree ntree = new Ntree();
        List<Integer> list = new ArrayList<>();
        list.add(1);
        ntree.insertNTree(ntree.root, list);
        list.clear();
        list.add(3);
        list.add(2);
        list.add(4);
        ntree.insertNTree(ntree.root, list);
        list.clear();
        list.add(5);
        list.add(6);
        ntree.insertNTree(ntree.root, list);

        List<List<Integer>> integers = ntree.levelNtree(ntree.root);
        System.out.println(11);
        integers.forEach(item -> {
                    item.forEach(item1 ->
                            System.out.print(item1 + ","));
                }
        );
    }

    //递归构建 n叉树
    public void insertNTree(NTreeNode treeNode, List<Integer> nodeList) {
        if (nodeList.size() == 0) {
            return;
        }
        List<NTreeNode> newNodeList = new ArrayList<>();

        // 根节点初始化
        if (root == null) {
            NTreeNode newNode = new NTreeNode(nodeList.get(0));
            newNode.setNodeList(new ArrayList<>());
            root = newNode;
            return;
        }

        // 非根节点初始化
        List<NTreeNode> nTreeNodes = treeNode.getNodeList();
        if (nTreeNodes.size() == 0) {
            for (Integer node : nodeList) {
                NTreeNode singelNode = new NTreeNode(node);
                newNodeList.add(singelNode);
                singelNode.setNodeList(new ArrayList<>());
            }
            treeNode.setNodeList(newNodeList);
            nodeList.clear();
            return;
        }

        // 当前节点有孩子，就用递归 将这些nodeList挂在其中一个空的子节点
        for (NTreeNode node : nTreeNodes) {
            insertNTree(node, nodeList);
        }
    }

    // N叉树的前序遍历
    public List<Integer> preNtree(NTreeNode nTreeNode) {
        List<Integer> res = new ArrayList<>();
        if (nTreeNode == null) {
            return res;
        }
        res.add(nTreeNode.getValue());
        for (NTreeNode node : nTreeNode.getNodeList()) {
            res.addAll(preNtree(node));
        }
        return res;
    }

    // N叉树的后序遍历
    public List<Integer> afterNtree(NTreeNode nTreeNode) {
        List<Integer> res = new ArrayList<>();
        if (nTreeNode == null) {
            return res;
        }
        for (NTreeNode node : nTreeNode.getNodeList()) {
            res.addAll(afterNtree(node));
        }
        res.add(nTreeNode.getValue());
        return res;
    }

    // N叉树的层序遍历
    public List<List<Integer>> levelNtree(NTreeNode nTreeNode) {
        List<List<Integer>> res = new ArrayList<>();
        Deque<NTreeNode> deque = new LinkedList<>();
        if (nTreeNode == null) {
            return res;
        }
        deque.offer(nTreeNode);
        while (!deque.isEmpty()) {
            List<Integer> path = new ArrayList<>();
            for (int i = 0; i < deque.size(); i++) {
                NTreeNode poll = deque.poll();
                path.add(poll.getValue());
                for (NTreeNode node : nTreeNode.getNodeList()) {
                    deque.offer(node);
                }
            }
            res.add(path);
        }
        return res;
    }
}
